package com.fs.concurrentmodel.day7.threadandlock;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author shuai.fang
 * @date：2017-12-10
 * @description：
 */
public class Counting {

	public static void main(String[] args) throws InterruptedException {

		class Counter {
			private AtomicInteger count = new AtomicInteger(0);

			public  void increment() {
				count.incrementAndGet();
			}

			public int getCount() {
				return count.get();
			}
		}

		final Counter counter = new Counter();

		class CountingThread extends Thread {
			@Override
			public void run() {
				for (int i = 0; i < 10000; i++) {
					counter.increment();
				}
			}
		}

		CountingThread countingThread1 = new CountingThread();
		CountingThread countingThread2 = new CountingThread();
		countingThread1.start();
		countingThread2.start();
		countingThread1.join();
		countingThread2.join();
		System.out.println(counter.getCount());
	}

}
